package com.ruoyi.web.controller.byw;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.BywEmployee;
import com.ruoyi.system.service.IBywEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywEmployeeBillingDetails;
import com.ruoyi.system.service.IBywEmployeeBillingDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工结算Controller
 * 
 * @author ruoyi
 * @date 2024-05-25
 */
@Api("员工结算")
@RestController
@RequestMapping("/byw/employee-billing-details")
public class BywEmployeeBillingDetailsController extends BaseController
{
    @Autowired
    private IBywEmployeeBillingDetailsService bywEmployeeBillingDetailsService;

    @Autowired
    private IBywEmployeeService employeeService;

    /**
     * 查询员工结算列表
     */
    @ApiOperation("查询结算记录")
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:list')")
    @GetMapping("/list")
    public TableDataInfo list(BywEmployeeBillingDetails bywEmployeeBillingDetails)
    {
        startPage();
        List<BywEmployeeBillingDetails> list = bywEmployeeBillingDetailsService.selectBywEmployeeBillingDetailsList(bywEmployeeBillingDetails);
        return getDataTable(list);
    }

    /**
     * 导出员工结算列表
     */
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:export')")
    @Log(title = "员工结算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BywEmployeeBillingDetails bywEmployeeBillingDetails)
    {
        List<BywEmployeeBillingDetails> list = bywEmployeeBillingDetailsService.selectBywEmployeeBillingDetailsList(bywEmployeeBillingDetails);
        ExcelUtil<BywEmployeeBillingDetails> util = new ExcelUtil<BywEmployeeBillingDetails>(BywEmployeeBillingDetails.class);
        util.exportExcel(response, list, "员工结算数据");
    }

    /**
     * 获取员工结算详细信息
     */
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bywEmployeeBillingDetailsService.selectBywEmployeeBillingDetailsById(id));
    }

    /**
     * 新增员工结算
     */
    @ApiOperation("员工-新建结算")
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:add')")
    @Log(title = "员工结算", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody BywEmployeeBillingDetails bywEmployeeBillingDetails)
    {
        BywEmployee bywEmployee = employeeService.selectBywEmployeeById(bywEmployeeBillingDetails.geteId());
        if (bywEmployee == null){
            return error("当前员工不存在");
        }
        if((bywEmployee.getUnlockedNum() - bywEmployee.getSettledNum()) < bywEmployeeBillingDetails.getVitaeNum()){
            return error("当前可结算简历数不足");
        }

        bywEmployee.setSettledNum(bywEmployee.getSettledNum() + bywEmployeeBillingDetails.getVitaeNum());
        bywEmployee.setSettledMoney(bywEmployee.getSettledMoney().add(new BigDecimal(bywEmployeeBillingDetails.getMoney())));
        bywEmployee.setToSettleMoney(bywEmployee.getToSettleMoney().subtract(new BigDecimal(bywEmployeeBillingDetails.getMoney())));
        bywEmployee.setUpdateBy(getUsername());
        bywEmployee.setUpdateTime(DateUtils.getNowDate());
        employeeService.updateBywEmployee(bywEmployee);

        bywEmployeeBillingDetails.setCreateBy(getUsername());
        return toAjax(bywEmployeeBillingDetailsService.insertBywEmployeeBillingDetails(bywEmployeeBillingDetails));
    }

    /**
     * 修改员工结算
     */
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:edit')")
    @Log(title = "员工结算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BywEmployeeBillingDetails bywEmployeeBillingDetails)
    {
        return toAjax(bywEmployeeBillingDetailsService.updateBywEmployeeBillingDetails(bywEmployeeBillingDetails));
    }

    /**
     * 删除员工结算
     */
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:remove')")
    @Log(title = "员工结算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bywEmployeeBillingDetailsService.deleteBywEmployeeBillingDetailsByIds(ids));
    }
}
